#!/usr/bin/env python
# -*- coding: utf-8 -*-

#作者: 徐晨阳
#时间: 2007-04-05

import pygtk
pygtk.require('2.0')
import gtk
from gtk import glade


class d_login:
	def __init__(self,check,login):
		'''
		w_login(check,login)用户登录界面
		check(user,password) -> boolean
		用户校验函数
		login()
		用户登录成功运行的函数
		'''
		self.check=check
		self.login=login
		#读取界面文件
		self.wtree = glade.XML("python.glade",'d_login')
		self.window=self.wtree.get_widget('d_login')
		#设置事件字典
		dict={'on_cancel_clicked':gtk.main_quit,
					'on_d_login_destroy':gtk.main_quit,
					'on_ok_clicked':self.ok_login,
					'on_user_key_press_event':self.key,
					'on_pass_key_press_event':self.key}
		self._tab={'e_user':'e_pass','e_pass':'b_ok','b_ok':'e_user'}
		#连接控件树
		self.wtree.signal_autoconnect(dict)
	def key(self,widget,event):
		'''
		键盘操作
		'''
		if event.keyval==gtk.keysyms.Return:
			self.tab(widget)
	def tab(self,widget):
		'''
		光标跳转
		'''
		obj = self.wtree.get_widget(self._tab[widget.name])
		#设置焦点
		obj.grab_focus()
	def ok_login(self,widget):
		e_user=self.wtree.get_widget('e_user')
		e_pass=self.wtree.get_widget('e_pass')
		if self.check(e_user.get_text(),e_pass.get_text()):
			self.window.hide()
			self.login()
		else:
			l=self.wtree.get_widget('label1')
			l.set_text('用户登录错误，请重新输入:')
			self.tab(widget)
def check(user,password):
	'''校验用户密码的函数'''
	return user=='xcy' and password=='123'
def login():
	'''用户登录成功运行的程序'''
	main=w_main()
	main.add('www')
class w_main:
	def __init__(self):
		#读取界面文件
		self.wtree = glade.XML("python.glade",'w_main')
		self.win=self.wtree.get_widget('w_main')
		#设置事件字典
		dict={'on_w_main_destroy':gtk.main_quit}
		#连接控件树
		self.wtree.signal_autoconnect(dict)
		#定义菜单树
		self.t_menu=self.wtree.get_widget("t_menu")
		#设置数据
		self.store = gtk.TreeStore(str,int)
		#定义选择事件
		self.selection = self.t_menu.get_selection()
		#改变选择运行select on_t_menu_select_cursor_row
		self.selection.connect('changed',self.select)
		#列1
		col1 = gtk.TreeViewColumn('名称')
		#增加列名
		self.t_menu.append_column(col1)
		#设置单元类型
		cell = gtk.CellRendererText()
		col1.pack_start(cell, True)
		#单元类型为text
		col1.add_attribute(cell, 'text', 0)
		#列2
		col2 = gtk.TreeViewColumn('说明')
		#增加列2
		self.t_menu.append_column(col2)
		cell = gtk.CellRendererText()
		col2.pack_start(cell, True)
		col2.add_attribute(cell,'text', 1)

		self.t_menu.set_search_column(0)
		col1.set_sort_column_id(0)
		#self.t_menu.set_reorderable(True)				
		self.t_menu.set_model(self.store)
	def add(self,name):
		for parent in range(4):
			piter = self.store.append(None, ['parent %i' % parent,parent])
			for child in range(3):
				self.store.append(piter, ['child %i of parent %i' %(child, parent),child])
	def select(self,obj):
		print 'select'
if __name__ == "__main__":
	log = d_login(check,login)
	#login()	
	gtk.main()
	#显示窗口
	#测试